<template>
  <div>
    
  </div>
</template>
<script>

import { domain, CreateWorkOrderFrom, GetWorkOrderFromInfo, URLP, orderstatus } from './../../libs/api_v1'
import { freemem } from 'os';
import { setTimeout } from 'timers';
export default {
    name:"WorkOrderFrom",
    data:()=>({
        name:'',
        contact:'',
        time:"9:00-18:00",
        title:'',
        desc:'',
        subing:false,
        file:{
            audio:null,
            image:null
        },
    }),
    methods:{
        timeIntervalS1: function(){
            this.timeInterval.S1=true
            this.timeInterval.S2=false
        },
        timeIntervalS2: function(){
            this.timeInterval.S1=false
            this.timeInterval.S2=true
        },
        timeIntervalS3: function(){
            this.timeInterval.S2=false
            this.time = this.timeInterval.start + "-" + this.timeInterval.end
        },
        toUserCenter:function(){
           window.location.href = "/user/self"
        },
        submitfromdata:function(){ 
            if(this.name.length == '' || this.contact.length == '' || this.title.length == '' || this.desc.length == ''){
              this.$store.commit('setAlert',{icon:'close',text:"",show:true})
              this.$notify({ type: 'warning', message: '输入不完整' });
              return
            }
            this.subing = true
            const sub = (data)=>{
                CreateWorkOrderFrom(URLP('order_no'),data,this.$cookies.get("authtoken"))
                .then((resp)=>{
                    var res = resp.data;
                    if (res.code == 1){
                      window.location.href = domain +"/workorder/message?order_no="+URLP('order_no')+"&hl="+URLP("hl")
                    }
                    this.$store.commit('setAlert',{icon:'close',text:res.msg,show:true})
                    this.subing =false
                })
                .catch((err)=>{
                  console.log(err)
                  this.subing =false
                  this.$store.commit('setAlert',{icon:'close',text:err,show:true})
                })
                
            }
            const upimage = () => {
                console.log("上传图片",this.wxlocad.image)
                var arr = []
                var wxim = this.wxlocad.image
                var ai = 0
                const setupdatep = (res) => {
                    var serverId = res.serverId; // 返回图片的服务器端ID serverId 即 media_id。
                        arr[ai] = serverId
                        console.log("arr.length:",arr.length,"wxim.length:",wxim.length)
                    if(arr.length == wxim.length){
                        this.wxupdate.image.server_id = arr
                        this.wxupdate.image.status = true //  上传成功
                    }else{
                        ai++
                        wx.uploadImage({
                            localId: wxim[ai], // 需要上传的图片的本地ID，由chooseImage接口获得
                            isShowProgressTips: 1, // 默认为1，显示进度提示
                            success: setupdatep
                        });
                    }
                    console.log("wxlo",this.wxlocad.image ,"wxup",this.wxupdate.image.server_id)
                }
                wx.uploadImage({
                    localId: wxim[ai], // 需要上传的图片的本地ID，由chooseImage接口获得
                    isShowProgressTips: 1, // 默认为1，显示进度提示
                    success: setupdatep
                });
            }
            const upaudio = () => {
                const setupdatepserverId =(serverId)=>{
                    this.wxupdate.audio.server_id = serverId
                    this.wxupdate.audio.status = true
                }
                wx.uploadVoice({
                    localId: this.wxlocad.audio, // 需要上传的音频的本地ID，由stopRecord接口获得
                    isShowProgressTips: 1, // 默认为1，显示进度提示
                    success: function (res) {
                        var serverId = res.serverId // 返回音频的服务器端ID
                        setupdatepserverId(serverId)
                    }
                });
            }

            if( this.wxlocad.image != null && this.wxlocad.audio != null) { // 图片和音频都存在
                this.have = 1
                upimage()
                upaudio()
            }
            if( this.wxlocad.image != null && this.wxlocad.audio == null ) {// 上传图片
                this.have = 2
                upimage()
            }
            if( this.wxlocad.image == null && this.wxlocad.audio != null ) {// 上传录音
                this.have = 3
                upaudio()
            }
            
            const isupsub= () =>{
                if( this.have == 1){
                    if(this.wxupdate.audio.status && this.wxupdate.image.status){
                        sub({
                            name:this.name,
                            contact:this.contact,
                            time:this.time,
                            title:this.title,
                            desc:this.desc,
                            audio:this.wxupdate.audio.server_id,
                            images:this.wxupdate.image.server_id
                        })
                    }else{
                        setTimeout(isupsub,200)
                    }
                }
                if( this.have == 2 ){ // 上传图片
                    if( this.wxupdate.image.status ){
                        sub({
                            name:this.name,
                            contact:this.contact,
                            time:this.time,
                            title:this.title,
                            desc:this.desc,
                            audio:this.wxupdate.audio.server_id,
                            images:this.wxupdate.image.server_id
                        })
                    }else{
                        setTimeout(isupsub,200)
                    }
                }
                if( this.have == 3 ){
                    if( this.wxupdate.audio.status ){
                        sub({
                            name:this.name,
                            contact:this.contact,
                            time:this.time,
                            title:this.title,
                            desc:this.desc,
                            audio:this.wxupdate.audio.server_id,
                            images:this.wxupdate.image.server_id
                        })
                    }else{
                        setTimeout(isupsub,200)
                    }
                }
                 if( this.have == 0 ){
                     sub({
                            name:this.name,
                            contact:this.contact,
                            time:this.time,
                            title:this.title,
                            desc:this.desc,
                            audio:this.wxupdate.audio.server_id,
                            images:this.wxupdate.image.server_id
                        })
                 }
            }
            isupsub()
            console.log("data:",{
                            name:this.name,
                            contact:this.contact,
                            time:this.time,
                            title:this.title,
                            desc:this.desc,
                            audio:this.wxupdate.audio.server_id,
                            images:this.wxupdate.image.server_id
                        },"have:",this.have,"is:",this.wxupdate.image.status,"as:",this.wxupdate.audio.status)
        },
        startRecord: function(){ // 开始录音
            this.RecordProgress = 0
            this.tmo = 0
            const progress = () => {
                this.tmo+=0.1
                this.RecordProgress = 100/60 * this.tmo
                if(this.tmo>59){
                    clearInterval(cp_time)
                }
                console.log(this.tmo)
            }
            window.cp_time = setInterval(progress, 100)
            const hideRecord =()=>{
                this.showRecord = false
                clearInterval(cp_time)
            }
            this.showRecord = true
            const setaudiolocalId = (localId) => {
                this.wxlocad.audio = localId
            }
            wx.startRecord() // 开始录音
            wx.onVoiceRecordEnd({
            // 录音时间超过一分钟没有停止的时候会执行 complete 回调
                complete: function (res) {
                    var localId = res.localId;
                    setaudiolocalId(localId)
                    hideRecord()
                }
            });
        },
        stopRecord:function(){ // 停止录音
           clearInterval(window.cp_time)
            this.showRecord = false
            const setaudiolocalId = (localId) => {
                this.wxlocad.audio = localId
            }
            wx.stopRecord({
                success: function (res) {
                    var localId = res.localId
                    setaudiolocalId(localId)
                }
            });
        },
        refreshRecord:function(){
            this.wxlocad.audio = null
        },
        selectimages: function(){ // 选择图片
            const show = (lids) => { // 显示预览
                this.wxlocad.image = lids
            }
            // 使用微信SDK上传
            wx.chooseImage({ // 选择图片
                count: 9, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                    show(localIds)
                }
            });
        },
        startPlayRecord:function () {
            wx.playVoice({
                localId: this.wxlocad.audio // 需要播放的音频的本地ID，由stopRecord接口获得
            });
        },
        stopPlayRecord:function () {
            wx.stopVoice({
                localId: this.wxlocad.audio // 需要停止的音频的本地ID，由stopRecord接口获得
            });
        },
    },
    created: function(){
        this.$store.commit('setProgress',false)
        let order_no = URLP("order_no")
        if(order_no){ // 存在订单可以进行提交
            orderstatus(order_no,this.$cookies.get("authtoken"))
            .then((resp)=>{
                var res = resp.data
                if(res.code == 1){
                    if(!res.data.write_off){
                      console.log(res.data)
                    }else{
                      // this.$store.commit('setAlert',{icon:'close',textni:"此订单已被使用",show:true})
                      this.$store.commit('setAlert',{icon:'close',text:"此订单已被使用",show:true})
                      window.location.href = "/workorder/create/order"
                    }
                    // this.$store.commit('setAlert',{icon:'close',text:res.msg,show:true})
                }else{
                  this.$store.commit('setAlert',{icon:'close',text:res.err,show:true})
                  window.location.href = "/workorder/create/order"
                }
            })
            .catch((err)=>{
              this.$store.commit('setAlert',{icon:'close',text:err,show:true})
            })
        }else{
          this.$store.commit('setAlert',{icon:'close',text:"不存在订单号码",show:true})
          window.location.href = "/workorder/create/order"
        }
    }
}
</script>
<style>
.box-from{
    margin-top: 62px;
    top:20px;
    margin-left: 5%;
    padding: 0% 2%;
    /*box-shadow: 0 5px 15px rgba(0,0,0,0.08);*/
    box-shadow: 0px 3px 1px -2px rgba(0, 0, 0, 0.2), 0px 2px 2px 0px rgba(0, 0, 0, 0.14), 0px 1px 5px 0px rgba(0, 0, 0, 0.12);
    width: 90%;
    border-radius: 4px;
}
.box-from2{
    max-width: 90%;
    width: 90%;
    margin-top: 16px;
    margin-left: 5%;
    /*box-shadow: 0 5px 15px rgba(0,0,0,0.08);*/
}
.notext{
user-select: none; -webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;
}
</style>